package com.ewanghuiju.app.ui.my.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.my.RedBeanHomeContract;
import com.ewanghuiju.app.model.bean.response.RedBeanDetailsResponBean;
import com.ewanghuiju.app.model.bean.response.RuleBean;
import com.ewanghuiju.app.model.bean.response.WaterDetailsResponBean;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.presenter.my.RedBeanHomePresenter;
import com.ewanghuiju.app.ui.my.adapter.RedBeanListAdapter;
import com.ewanghuiju.app.ui.pintuan.activity.PintuanRuleActivity;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.StringUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.FixedAspectRatioLinerLayout;
import com.gyf.immersionbar.ImmersionBar;
import com.muddzdev.styleabletoast.StyleableToast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 分红豆页面
 * @time 2020/2/19
 */
public class RedBeanHomeActivity extends BaseActivity<RedBeanHomePresenter> implements RedBeanHomeContract.View {


    @BindView(R.id.tv_red_bean_num)
    TextView tvRedBeanNum;
    @BindView(R.id.iv_red_bean)
    ImageView ivRedBean;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.tv_stocks)
    TextView tvStocks;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_details_lab)
    TextView tvDetailsLab;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.tv_increase)
    TextView tvIncrease;
    @BindView(R.id.tv_sales_volume)
    TextView tvSalesVolume;
    @BindView(R.id.tv_my_red_bean_lab)
    TextView tvMyRedBeanLab;
    @BindView(R.id.tv_my_red_bean)
    TextView tvMyRedBean;
    @BindView(R.id.tv_increase_red_bean)
    TextView tvIncreaseRedBean;
    @BindView(R.id.tv_stocks_total)
    TextView tvStocksTotal;
    @BindView(R.id.layout_stocks_details)
    LinearLayout layoutStocksDetails;
    @BindView(R.id.fixlayout_redbean_main)
    FixedAspectRatioLinerLayout fixlayoutRedbeanMain;
    @BindView(R.id.relativelayout_my_redbean_num)
    RelativeLayout relativelayoutMyRedbeanNum;
    @BindView(R.id.mIv)
    ImageView mIv;
    @BindView(R.id.tv_stocks_num)
    TextView tvStocksNum;


    private int currentPage = 1;
    private List<RedBeanDetailsResponBean> list;
    private RedBeanListAdapter redBeanListAdapter;
    private String number;
    private float waterPoundage;

    private int mType;

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }


    @Override
    protected int getLayout() {
        return R.layout.activity_red_bean_home;
    }

    @Override
    protected void initEventAndData() {
        mType = getIntent().getIntExtra(Constants.WATER_REDBEAN_TYPE, 1);
        if (mType == 1) {
            //分红豆
            mToolbar.setNavigationIcon(R.mipmap.login_fan);
            tvTitleRight.setTextColor(ContextCompat.getColor(mContext,R.color.color_666));

            fixlayoutRedbeanMain.setVisibility(View.VISIBLE);
            relativelayoutMyRedbeanNum.setVisibility(View.GONE);
            tvIncrease.setVisibility(View.GONE);
            tvStocks.setVisibility(View.GONE);
            tvNumber.setVisibility(View.GONE);
            mIv.setVisibility(View.GONE);

            tvTitle.setText(getString(R.string.pintuan_order_fhd));
            tvDetailsLab.setText("分红豆明细");
//            tvIncrease.setText("立即转化");
//            ivRedBean.setImageResource(R.mipmap.icon_red_bean);
        } else {
            mToolbar.setNavigationIcon(R.mipmap.icon_fan_white);
            tvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.white));
            tvTitle.setText(getString(R.string.pintuan_order_water));
            tvDetailsLab.setText("水滴明细");
            tvIncrease.setText("立即转赠");
            ivRedBean.setImageResource(R.mipmap.icon_water);
        }

        mToolbar.setBackground(null);
        tvTitleRight.setText("规则");
        tvTitleRight.setVisibility(View.VISIBLE);
        intList();
    }


    @OnClick({R.id.tv_increase, R.id.tv_title_right, R.id.tv_increase_red_bean})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {
            //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_increase:
                //转增
                new StartActivityUtil(mContext, WaterAndRedbeanIncreaseActivity.class).putExtra(Constants.WATER_REDBEAN_TYPE, mType)
                        .putExtra(Constants.WATER_INCREASE_NUMBER, number)
                        .putExtra(Constants.WATER_INCREASE_POUNDAGE, waterPoundage)
                        .startActivity(true);
                break;
            case R.id.tv_title_right:
                //拼团规则
                LoadingDialogUtils.show(mContext);
                //3 水滴 4分红豆
                mPresenter.getPtiguize(mType == 1 ? "4" : "3");
                break;
            case R.id.tv_increase_red_bean:
                //转增
                new StartActivityUtil(mContext, WaterAndRedbeanIncreaseActivity.class).putExtra(Constants.WATER_REDBEAN_TYPE, mType)
                        .putExtra(Constants.WATER_INCREASE_NUMBER, number).startActivity(true);
                break;
            default:
                break;
        }
    }


    /**
     * 加载列表
     */
    private void intList() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        redBeanListAdapter = new RedBeanListAdapter(R.layout.item_red_bean_details);
        recyclerView.setAdapter(redBeanListAdapter);

        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage++;
                mPresenter.getData(currentPage, mType);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage = 1;
                refreshLayout.resetNoMoreData();
                mPresenter.getData(currentPage, mType);
            }
        });
        //首次进入显示
        LoadingDialogUtils.show(mContext);
        currentPage = 1;
        mPresenter.getData(currentPage, mType);

        if (mType == 2) {
            mPresenter.getPtistocksInfo();
        }
    }

    @Override
    public void showPtiguize(RuleBean bean) {
        //获取规则成功
        LoadingDialogUtils.dismissDialog_ios();
        if (bean == null) {
            StyleableToast.makeText(mContext, "暂无数据", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        new StartActivityUtil(mContext, PintuanRuleActivity.class)
                .putExtra(Constants.RULE_TITLE, bean.getIntroduce())
                .putExtra(Constants.RULE_CONTENT, bean.getContent())
                .startActivity(true);

    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
//        ImmersionBar.with(this).statusBarView(R.id.top_view)
//                .init();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void intSmartRefreshLayout() {
//        super.intSmartRefreshLayout();
    }

    @Override
    public void showContent(RedBeanDetailsResponBean bean) {
        LoadingDialogUtils.dismissDialog_ios();
        if (bean == null) {
            StyleableToast.makeText(mContext, "信息获取失败", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        number = bean.getRed_bean();
        tvMyRedBean.setText(TextUtils.isEmpty(bean.getRed_bean()) ? "" : bean.getRed_bean());
        tvStocksTotal.setText("平台当前共计" + bean.getTocks_sum() + "股  您有");
        tvStocksNum.setText(TextUtils.isEmpty(bean.getStocks()) ? "0" : bean.getStocks());
        list = bean.getDetail();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishRefreshWithNoMoreData();
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishRefresh();
            }
            redBeanListAdapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            redBeanListAdapter.addData(list);
        }

    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
    }

    @Override
    public void increaseSuccess() {

    }

    @Override
    public void showWaterDetails(WaterDetailsResponBean bean) {
        if (bean == null) {
            StyleableToast.makeText(mContext, "信息获取失败", Toast.LENGTH_SHORT, R.style.mytoast).show();
            return;
        }
        number = bean.getIntegral();
        waterPoundage = bean.getPoundage();
        tvRedBeanNum.setText(TextUtils.isEmpty(bean.getIntegral()) ? "0" : bean.getIntegral());
        if (StringUtil.getFloatByString(bean.getFreeze_integral()) == 0) {
            tvStocks.setVisibility(View.INVISIBLE);
            tvNumber.setVisibility(View.INVISIBLE);
        } else {
            tvStocks.setVisibility(View.VISIBLE);
            tvNumber.setVisibility(View.VISIBLE);
            tvStocks.setText("您有" + bean.getFreeze_integral() + "个水滴处于锁定状态");
            tvNumber.setText("*您还需要拼团中奖" + bean.getFreeze_sum() + "次可解锁");
        }
    }

    @Override
    public void ptiredeanProportion(BaseResponse bean) {

    }

    @Override
    public void showWaterContent(List<RedBeanDetailsResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        //获取水滴明细成功
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishRefreshWithNoMoreData();
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishRefresh();
            }
            redBeanListAdapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            redBeanListAdapter.addData(list);
        }

    }

    @Override
    public void refreshData() {
        //刷新数据
        currentPage = 1;
        refreshLayout.resetNoMoreData();
        mPresenter.getData(currentPage, mType);
        if (mType == 2) {
            mPresenter.getPtistocksInfo();
        }
    }
}
